{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 作业一：\n",
    "# 复习矩阵相关操作，并完成下题:\n",
    "\n",
    "# 菜价\n",
    "# 日期          白菜      土豆      冬瓜\n",
    "# 2019/7/28     1.2       1.5      1.8\n",
    "# 2019/7/29     1.3       1.4      1.9\n",
    "# 2019/7/30     1.1       1.6      1.7\n",
    "\n",
    "# 每天采购数量相等，分别如下\n",
    "# 品名    数量\n",
    "# 白菜     5\n",
    "# 土豆    10\n",
    "# 冬瓜     9\n",
    "\n",
    "# 上面的数据，可以在numpy中表示如下 5分\n",
    "# X = np.array([ [1.2, 1.5, 1.8],\n",
    "#                [1.3, 1.4, 1.9],\n",
    "#                [1.1, 1.6, 1.7]\n",
    "#              ])\n",
    "# y = np.array([5, 10, 9]).T\n",
    "\n",
    "# 1. 使用循环的方式计算每天的采购总金额 得到结果为[37.2, 37.6, 36.8]，\n",
    "#    分别表示7/28、7/29、7/30这三天采购总额 10分\n",
    "# 2. 使用矩阵点乘来计算每天的采购总金额（使用np.dot来实现矩阵相乘） 5分\n",
    "# 3. 测试两种方式的性能 10分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>白菜</th>\n",
       "      <th>土豆</th>\n",
       "      <th>冬瓜</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019/7/28</th>\n",
       "      <td>1.2</td>\n",
       "      <td>1.5</td>\n",
       "      <td>1.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019/7/29</th>\n",
       "      <td>1.3</td>\n",
       "      <td>1.4</td>\n",
       "      <td>1.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019/7/30</th>\n",
       "      <td>1.1</td>\n",
       "      <td>1.6</td>\n",
       "      <td>1.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            白菜   土豆   冬瓜\n",
       "2019/7/28  1.2  1.5  1.8\n",
       "2019/7/29  1.3  1.4  1.9\n",
       "2019/7/30  1.1  1.6  1.7"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义np二维数组(3行3列), 一行代表1天内3种蔬菜的价格，一列代表1种蔬菜在每天的价格\n",
    "X = np.array([[1.2, 1.5, 1.8],\n",
    "              [1.3, 1.4, 1.9],\n",
    "              [1.1, 1.6, 1.7]\n",
    "              ])\n",
    "\n",
    "# 显示每日价目表\n",
    "price = pd.DataFrame(X, columns=['白菜', '土豆', '冬瓜'], index=['2019/7/28', '2019/7/29', '2019/7/30'])\n",
    "price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>购买数量</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>白菜</th>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>土豆</th>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>冬瓜</th>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    购买数量\n",
       "白菜     5\n",
       "土豆    10\n",
       "冬瓜     9"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义np一维数组,代表每天购买不同蔬菜的数量\n",
    "y = np.array([5, 10, 9]).T\n",
    "\n",
    "# 显示每日采购数量表\n",
    "quantity = pd.DataFrame(y, columns=['购买数量'], index=['白菜', '土豆', '冬瓜'])\n",
    "quantity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用循环的方式计算每天的采购总金额\n",
    "def loop_calc(X, y):\n",
    "    result = []\n",
    "    for row in range(X.shape[0]):               # 按行数循环\n",
    "        sum = 0\n",
    "        for col in range(X.shape[1]):           # 按列数循环\n",
    "            sum = sum + X[row, col] * y[col]\n",
    "        result.append(sum)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用矩阵点乘来计算每天的采购总金额（使用np.dot来实现矩阵相乘）\n",
    "def numpy_calc(X, y):\n",
    "    return np.dot(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用循环的方式计算每天的采购总金额:\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[37.2, 37.599999999999994, 36.8]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('使用循环的方式计算每天的采购总金额:')\n",
    "loop_calc(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用循环的方式计算每天的采购总金额，并用Pandas.DataFrame显示结果:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>采购总金额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019/7/28</th>\n",
       "      <td>37.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019/7/29</th>\n",
       "      <td>37.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019/7/30</th>\n",
       "      <td>36.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           采购总金额\n",
       "2019/7/28   37.2\n",
       "2019/7/29   37.6\n",
       "2019/7/30   36.8"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示采购总金额表\n",
    "cost = pd.DataFrame(loop_calc(X, y), columns=['采购总金额'], index=['2019/7/28', '2019/7/29', '2019/7/30'])\n",
    "print('使用循环的方式计算每天的采购总金额，并用Pandas.DataFrame显示结果:')\n",
    "cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用矩阵点乘来计算每天的采购总金额:\n",
      "[37.2 37.6 36.8]\n"
     ]
    }
   ],
   "source": [
    "print('使用矩阵点乘来计算每天的采购总金额:')\n",
    "print(np.dot(X, y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用矩阵点乘来计算每天的采购总金额，，并用Pandas.DataFrame显示结果:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>采购总金额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019/7/28</th>\n",
       "      <td>37.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019/7/29</th>\n",
       "      <td>37.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019/7/30</th>\n",
       "      <td>36.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           采购总金额\n",
       "2019/7/28   37.2\n",
       "2019/7/29   37.6\n",
       "2019/7/30   36.8"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示采购总金额表\n",
    "cost = pd.DataFrame(numpy_calc(X, y), columns=['采购总金额'], index=['2019/7/28', '2019/7/29', '2019/7/30'])\n",
    "print('使用矩阵点乘来计算每天的采购总金额，，并用Pandas.DataFrame显示结果:')\n",
    "cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用循环方式的计算时间是:\n",
      "7.33 µs ± 24.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "print('使用循环方式的计算时间是:')\n",
    "%timeit loop_calc(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用矩阵点乘的计算时间是:\n",
      "1.53 µs ± 7.55 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "print('使用矩阵点乘的计算时间是:')\n",
    "%timeit numpy_calc(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
